<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="linkList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" :label="$t('组名称')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.name" :content="scope.row.name" class="wid190" :refName="scope.row.name">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="physical" :label="$t('物理接口')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :html="physicalFormatter(scope.row)" :content="physicalFormatter(scope.row)" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="group_status" :label="$t('组状态')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.group_status" :content="scope.row.group_status" class="wid190" :refName="scope.row.group_status">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="switch" :label="$t('开关')" align='center' min-width="100">
        <template slot-scope="scope">
          {{switchArray[scope.row.switch]}}
        </template>
      </el-table-column>
      <el-table-column prop="member" :label="$t('接口状态')" align='center' min-width="200">
        <template slot-scope="scope">
          <tooltip :html="classMapShow(scope.row,';<br/>')" :content="classMapShow(scope.row,' ')" class="wid190" :refName="scope.row.name"></tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' + scope.$index">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination> -->

    <link-dialog v-if="linkDialogVisible" ref="refLinkDialogVisible"></link-dialog>
  </div>
</template>
<script>
import { getArray, delLinkInfo } from '@/api/network/api/link'
import LinkDialog from './components/LinkDialog'
// import 'view-design/dist/styles/iview.css'
export default {
  name: 'Link',
  components: {
    LinkDialog
  },
  data () {
    return {
      linkDialogVisible: false,
      linkList: [],
      // record: {
      //   page: 1,
      //   limit: 20
      // },
      switchArray: {
        'on': this.$t('开'),
        'off': this.$t('关')
      },
      nameStr: ''
      // total: 0
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // handleSizeChange (val) {
    //   // console.log(`每页 ${val} 条`)
    //   this.record.limit = val
    //   this.listData()
    // },
    // handleCurrentChange (val) {
    //   // console.log(`当前页: ${val}`)
    //   this.record.page = val
    //   this.listData()
    // },
    // 列表
    listData () {
      getArray().then(res => {
        if (res.status === 0) {
          // this.total = res.count
          this.linkList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.linkDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refLinkDialogVisible.init(data)
      })
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delLinkInfo({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    physicalFormatter (data) {
      let dataList = ''
      data.member.forEach(item => {
        if (dataList === '') {
          dataList = item.interface
        } else {
          dataList += ',' + item.interface
        }
      })
      return dataList
    },
    classMapShow (data, str) {
      let reStr = ''
      if (data.member) {
        data.member.forEach(val => {
          reStr += val.interface + ':' + val.status
          reStr += str
        })
        return reStr
      }
    }
  }
}
</script>
